<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="./fckeditor/fckeditor.js"></script>
<script language="javascript">
function Comet(url)
{
	this.url=url;
	this.error=true;
	this.timestamp=0;
	this.cur_user;
	this.doRequest=function(word)
	{
		word=escape(word);
		$.ajax({
		  type: "POST",
		  url: this.url+"?ran="+Math.random()+"&cur_user="+this.cur_user,
		  data:'msg='+word
		});
		
	};
}

 var comet=new Comet("backend_admin.php");
var comet_connect=function()
{
	$.ajax({
  type: "POST",
  url: comet.url+"?ran="+Math.random(),
  cache: false,
  dataType:'json',
  data:'timestamp='+comet.timestamp,
  success: function(response)
  {
	  comet.error=false;
	  comet.timestamp=response.timestamp;
	  	$("#results").prepend("<div>"+response.msg+"</div>");
	 
  },
  complete:function(response)
  {
	  if(comet.error)
	  {
		   setTimeout(function(){comet_connect() }, 5000); 
	  }
	  else
	  {
		
		  comet_connect();
		
	  }
  }
});
}
$(document).ready(function(){
 
  comet_connect();
  $("#member_2").css("display","none");
  changeAllMember();
  var word = new FCKeditor( 'word' ) ;
word.Width="500px";
word.Height="200px";
word.BasePath = "./fckeditor/" ;
word.ToolbarSet="Basic";
word.ReplaceTextarea() ;
}); 

function sendMsg()
{
	var word="Administrator";
	var time=new Date();
	word+='&nbsp;'+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds()+"<br>";
	word+=getEditorHTMLContents("word");
	comet.doRequest(word);
	SetEditorContents("word","");
	$("#results").prepend("<div>"+word+"</div>");
}
function changeChatUser(user)
{
	$("#chat_members li").css("background-color","");
	document.getElementById(user).style.backgroundColor="#F00";
	comet.cur_user=user;
	
	$("#send_button").attr("disabled","");
}
var cur_user="";
function changeAllMember()
{
	$.ajax({
  type: "POST",
  url: comet.url+"?ran="+Math.random()+"&act=user",
  cache: false,
  dataType:'json',
  data:'timestamp='+comet.timestamp,
  success: function(response)
  {
	  var add_user=response.add_user;
	  var remove_user=response.remove_user;
	  if(remove_user.length>0)
	  {
		  var users=remove_user;
		  users=users.split(",");
		  for(i=0;i<users.length;i++)
		  {
			  $("#"+users[i]).css("display","none");
		  }
	  }
	  if(add_user.length>0)
	  {
		   var users=add_user;
		  users=users.split(",");
		  for(i=0;i<users.length;i++)
		  {
			  if($("#"+users[i]).length>0)
			  {
				  $("#"+users[i]).css("display","");
			  }
			  else
			  {
				  var new_li='<li id="'+users[i]+'" onclick="changeChatUser(\''+users[i]+'\');">'+users[i]+'</li>';
				  $("#chat_members_ul").append(new_li);
			  }
		  }
	  }
  },
  complete:function(response)
  {
	  changeAllMember();
  }
});
}

function getEditorHTMLContents(EditorName) {
    var oEditor = FCKeditorAPI.GetInstance(EditorName);
    return(oEditor.GetXHTML(true));
}

function SetEditorContents(EditorName, ContentStr) {
    var oEditor = FCKeditorAPI.GetInstance(EditorName) ;
    oEditor.SetHTML(ContentStr) ;
}
</script>
<style>
body{
	margin-left:auto; 
	margin-right:auto; 
	width:500px;
}
#chat_members
{
	width:200px; 
	float:left;
}
#chat_members ul{
	list-style:none;
}
#chat_members li
{
	padding-top:10px; 
	text-indent:40px;
	height:40px;  
	background-image:url(../images/chat_header.gif); 
	background-position: top left; 
	background-repeat:no-repeat;
	cursor:pointer;
}
</style>
</head>

<body>
<div id="chat_members">
	<ul id="chat_members_ul">
	<?php 
	$members=scandir(dirname(__FILE__)."/chat/");
	array_shift($members);
	array_shift($members);
	if(!empty($members))
	{
		foreach ($members as $member)
		{
			
	
	?>
    	<li id="<?php echo $member; ?>"  onclick="changeChatUser('<?php echo $member; ?>');"><?php echo $member; ?></li>
    	<?php
    		}
	} 
    	?>
    </ul>
</div>
<div style=" float:right; width:300px;">
<div id="results" style="height:300px;overflow-y:scroll;"></div>
</div>
<div style=" clear:both">
<form action="" method="get" >
    <textarea name="word" id="word"></textarea>
    <input type="button" name="button" id="send_button" onclick="sendMsg()" value="Send"  disabled="disabled"/>
  </form>
</div>
</body>
</html>